<?php

class Seatting_falin extends FrontEnd_Controller {

    function Seatting_falin() {
        parent::FrontEnd_Controller();
        $this->load->model('Home_m');
        $this->load->model('Seatting_admin_m');
        $this->load->helper('date');
        $this->load->library('email');
    }

    function index() {
        $this->display_view('home/index');
    }

    function ani_video() {
        $per_page = $this->uri->segment(3) != '' ? $this->uri->segment(3) : 10;
        $off_set = $this->uri->segment(4) != '' ? $this->uri->segment(4) : 0;
        $config['base_url'] = base_url() . 'index.php/home/ani_video/' . $per_page . '/';
        $this->session->set_userdata('smart_navigate_url', $config['base_url'] . $off_set);
        $config['per_page'] = $per_page;
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Video_m->count_all_video();
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['own_walls'] = $this->Home_m->get_all_videos($per_page, $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->display_view('home/compte');
    }

    function list_recommandation() {
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 10;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/seatting_falin/list_recommandation/' . '/' . $per_page . '/';
        $nav_link = $this->session->set_userdata('smart_navigate_url', site_url('seatting_falin/list_recommandation'));
        $config['per_page'] = '10';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Home_m->count_recommandation();
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['list_recommandation'] = $this->Home_m->get_all_recommandation($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Home_m->count_recommandation();
        $this->_data['page_title'] = "Liste de recommandation";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('recommandation/list_feedback');
    }

    function feedback() {
        if ($this->input->post('submit')) {
            $formVal = array(
                array(
                    'field' => 'content',
                    'Label' => 'Content',
                    'rules' => 'required'
                )
            );
            $this->form_validation->set_rules($formVal);

            if ($this->form_validation->run() == TRUE) {
                $data = array(
                    'title' => $this->input->post('title'),
                    'content' => $this->input->post('content'),
                    'feedback_date' => now(),
                    'active' => 0,
                    'user_id' => $this->session->userdata('user_id')
                );
                $this->Home_m->feedback($data);
                redirect('seatting_falin/list_recommandation');
            } else {
                $this->display_view('seatting_falin/list_recommandation');
            }
        } else {
            $this->display_view('seatting_falin/list_recommandation');
        }
    }

    function list_gardient() {
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 10;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/seatting_falin/list_gardient/' . '/' . $per_page . '/';
        $nav_link = $this->session->set_userdata('smart_navigate_url', site_url('seatting_falin/list_gardient'));
        $config['per_page'] = '10';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Home_m->count_gardient();
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['query'] = $this->Home_m->list_gardient($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Home_m->count_gardient();
        $this->_data['page_title'] = "Liste de gardient";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('gardient/list_gardient');
    }

    function search_gardient() {
        if ($this->input->post('search')) {
            $city = $this->input->post('search_city');
            $this->_data['selected_region'] = $city;
            if ($city != null && $city != '') {
                $per_page = $this->uri->segment(3);
                if ($per_page == "") {
                    $per_page = 10;
                }
                $off_set = $this->uri->segment(4);
                if ($off_set == "") {
                    $off_set = 0;
                }
                $config['base_url'] = base_url() . 'index.php/seatting_falin/search_gardien/' . $city . '/' . $per_page . '/';
                $config['per_page'] = $per_page;
                $config['uri_segment'] = 4;
                $config['total_rows'] = $this->Home_m->count_search_gardient($city);
                if ($off_set > 0 && $off_set == $config['total_rows']) {
                    $off_set = $off_set - $per_page;
                }
                $this->_data['query'] = $this->Home_m->search_gardient($city, $config['per_page'], $off_set);
                $this->pagination->initialize($config);
                $this->_data['pagination'] = $this->pagination->create_links();
                $this->_data['total'] = $this->Home_m->count_search_gardient($city);
                $this->_data['page_title'] = "Liste de gardient";
                $this->_data['per_page'] = $per_page;
                $this->_data['off_set'] = $off_set;
                $this->_data['city_search'] = $city;
                $this->display_view('gardient/list_gardient');
            } else {
                $this->list_gardient();
            }
        } else {
            $this->display_view('gardient/list_gardient');
        }
    }

    function search_gardien() {
        $city = $this->uri->segment(3);
        $this->_data['selected_region'] = $city;
        $per_page = $this->uri->segment(4);
        if ($per_page == "") {
            $per_page = 10;
        }
        $off_set = $this->uri->segment(5);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/seatting_falin/search_gardien/' . $city . '/' . $per_page . '/';
        $config['per_page'] = $per_page;
        $config['uri_segment'] = 5;
        $config['total_rows'] = $this->Home_m->count_search_gardient($city);
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['query'] = $this->Home_m->search_gardient($city, $config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Home_m->count_search_gardient($city);
        $this->_data['page_title'] = "Liste de gardient";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->_data['city_search'] = $city;
        $this->display_view('gardient/list_gardient');
    }

    function booking_gardient($gardient_id) {
        $this->_data['gar_id'] = $gardient_id;
        $gardient_data = $this->Seatting_admin_m->get_detail_gardient($gardient_id);
        $this->_data['gardient_data'] = $gardient_data;
                if ($gardient_data->num_rows() > 0) {
                    $gardien = $gardient_data->row();
                    $gardien_name = $gardien->gardient_name;
                    $city = $gardien->city;
                }
        if ($this->input->post('submit')==TRUE) {
            $formVal = array(
                  array(
                    'field' => 'date_start',
                    'Label' => 'Date start',
                    'rules' => 'required'
                ),
                 array(
                    'field' => 'confirm_contact',
                    'Label' => 'confirm_contact',
                    'rules' => 'required'
                )
            );
            $this->form_validation->set_rules($formVal);

            $config['charset'] = 'utf-8';
            $config['wordwrap'] = TRUE;
            $config['mailtype'] = 'html';
            $this->email->initialize($config);

            if ($this->form_validation->run() == TRUE) {
                $ids = $this->input->post('habitat');
                $habitat = "";
                if (!empty($ids))
                {
                    $str=",";
                    for($i=0;$i<count($ids);$i++){
                        if($i==count($ids)-1){
                            $str='';
                        }
                        $habitat .= $ids[$i].$str;
                    }
                }
                $date_start_long = human_to_unix($this->input->post('date_start') . ' ' . '06:00');
                $data = array(
                    'title' => $this->input->post('title'),
                    'content' => $this->input->post('content'),
                    'date_start' => $date_start_long,
                    'total_date' => $this->input->post('total_date'),
                    'hour_start' => $this->input->post('hour_start'),
                    'total_hour' => $this->input->post('total_hour'),
                    'content' => $this->input->post('content'),
                    'animal_kind' => $this->input->post('animal'),
                    'total_animal' => $this->input->post('total_animal'),
                    'medicaments' => $this->input->post('medicaments'),
                    'known' => $this->input->post('known'),
                    'habitat' => $habitat,
                    'contact_date' => now(),
                    'gardient_id' => $gardient_id,
                    'status' => 0,
                    'user_id' => $this->session->userdata('user_id')
                );
                $this->Home_m->booking_gardient($data);
                $array= array( 'user_id' => $this->session->userdata('user_id'));
               $booking= $this->Home_m->get_booking_gardient($array);

               $this->session->set_userdata(array('seating_id_commande' =>$booking['id'] ));
              redirect('seatting_falin/paiement') ;
            } else {
                $this->display_view('gardient/booking_gardient');
            }
        } else {
            $this->display_view('gardient/booking_gardient');
        }
    }

    function paiement(){
        $user_data = $this->Home_m->get_user_by_id($this->session->userdata('user_id'));
          if ($user_data->num_rows() > 0) {
                    $user = $user_data->row();
                    $email=$user->email;
          }
        $id_commande=$this->session->userdata('seating_id_commande');
    echo "https://www.bluepaid.com/in.php?&id_boutique=15555www&id_client=105230&montant=4&devise=EUR&langue=FR&email_client=".$email."&divers=seatting|".$id_commande;

    }

    function contact() {
        if ($this->input->post('submit')==TRUE && $this->input->post('confirm_contact')=='1') {
            $formVal = array(
                array(
                    'field' => 'content',
                    'Label' => 'Content',
                    'rules' => 'required'
                ),
                array(
                    'field' => 'date_start',
                    'Label' => 'Date start',
                    'rules' => 'required'
                )
            );
            $this->form_validation->set_rules($formVal);

            $config['charset'] = 'utf-8';
            $config['wordwrap'] = TRUE;
            $config['mailtype'] = 'html';
            $this->email->initialize($config);
            
            if ($this->form_validation->run() == TRUE) {
                $region = $this->Home_m->get_sub_region_name($this->input->post('city'));
                $this->_data['region'] = $region;
                if ($region->num_rows() > 0) {
                    $name = $region->row();
                    $sub_region_name = $name->name;
                }
                $ids = $this->input->post('habitat');
                $habitat = "";
                if (!empty($ids))
                {
                    $str=",";
                    for($i=0;$i<count($ids);$i++){
                        if($i==count($ids)-1){
                            $str='';
                        }
                        $habitat .= $ids[$i].$str;
                    }
                }

                $date_order_long = human_to_unix($this->input->post('date_start') . ' ' . '06:00');
                $data = array(
                    'title' => $this->input->post('title'),
                    'content' => $this->input->post('content'),
                    'date_start' => $date_order_long,
                    'total_date' => $this->input->post('total_date'),
                    'hour_start' => $this->input->post('hour_start'),
                    'total_hour' => $this->input->post('total_hour'),
                    'city' => $this->input->post('city'),
                    'content' => $this->input->post('content'),
                    'animal_kind' => $this->input->post('animal'),
                    'total_animal' => $this->input->post('total_animal'),
                    'add_fidelity' => $this->input->post('fidelity'),
                    'fidelity' => $this->input->post('fidelite_code'),
                    'medicaments' => $this->input->post('medicaments'),
                    'add_fidelity' => $this->input->post('add_fidelity'),
                    'known' => $this->input->post('known'),
                    'habitat' => $habitat,
                    'date' => now(),
                    'user_id' => $this->session->userdata('user_id')
                );
                $this->Home_m->contact($data);

                //send mail
                $user_data = $this->Home_m->get_user_by_id($this->session->userdata('user_id'));
                if ($user_data->num_rows() > 0) {
                    $user = $user_data->row();
                    $From = $user->email;
                    $to = 'dungkirimi@gmail.com';
                    $this->email->from($From, 'Seatting-falin');
                    $this->email->to($to);
                    $this->email->subject('Contact gardien');
                    $add_fi = $this->input->post('add_fidelity')==1?'Qui':'Non';
                    $medi = $this->input->post('medicaments')==1?'Qui':'Non' ;

                    $message = 'Hello ' . $user->username . ".<p> Il s'agit d'une des nouvelles commandes de Lehu</p>";
                    $message .= '<p style="font-weight:bold;">' . $this->input->post('title') . '</p>';
                    $message .= '<p>    - In city: ' . $sub_region_name . '</p>';
                    $message .= '<p>    - A partir de quelle date :' . $this->input->post('date_start') . '</p>';
                    $message .= '<p>    - Combien de jours : ' . $this->input->post('total_date') . ' jours</p>';
                    $message .= '<p>    - A partir de quelle hour: ' . $this->input->post('hour_start') . 'h</p>';
                    $message .= '<p>    - Combien de hours : ' . $this->input->post('total_hour') . 'h</p>';
                    $message .= '<p>    - Animal kind: ' . $this->input->post('animal') . '</p>';
                    $message .= '<p>    - Total animal: ' . $this->input->post('total_animal') . '</p>';
                    $message .= '<p>    - Voulez-vous recevoir votre carte de fidélité Seatting-falin+: ' .$add_fi. '</p>';
                    $message .= '<p>    - Si oui indiquer le numero ci desous:' . $this->input->post('fidelite_code') . '</p>';
                    $message .= '<p>    - Faudra t-il que le gardien lui administre des médicaments durant la mission: ' . $medi. '</p>';
                    $message .= '<p>    - Comment avez-vous connu Seatting-falin: ' . $this->input->post('known') . '</p>';
                    $message .= '<p>    - Votre/vos animaux présentent t-ils des caractéristiques particulières au niveau de son/leurs comportements: ' . $this->input->post('content') . '</p>';
                    $this->email->message($message);
                    //echo $message;die;
                    $this->email->send();
                    //end send
                }
                redirect('seatting_falin');
            } else {
                $this->display_view('home/contact');
            }
        } else {
            $this->display_view('home/contact');
        }
    }

    function detail_card_vip() {
        $query = $this->Home_m->get_card_vip();
        $this->_data['detail_card_vip'] = $query;
        $this->_data['page_title'] = "CARD VIP";
        $nav_link = $this->session->set_userdata('smart_navigate_url', site_url('seatting_falin/detail_card_vip'));
        $this->display_view('home/detail_card_vip');
    }

    function detail_protect_animal() {
        $query = $this->Home_m->get_protect_animal();
        $this->_data['detail_protect_animal'] = $query;
        $this->_data['page_title'] = "PROTECT ANIMAL";
        $nav_link = $this->session->set_userdata('smart_navigate_url', site_url('seatting_falin/detail_protect_animal'));
        $this->display_view('home/detail_protect_animal');
    }

}

?>
